package com.bizdroid.unutturma.utility;

import java.util.ArrayList;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationManager;
import android.preference.PreferenceManager;

import com.bizdroid.unutturma.model.ReminderItem;

public class Utility {

	public final static String SEPARATOR = ";";
	private static SharedPreferences prefs;
	private static LocationManager locationManager;;

	public static ReminderItem parse(String value) {
		String[] reminderItemValues = value.split(SEPARATOR);
		ReminderItem reminderItem = new ReminderItem();
		reminderItem.setKey(reminderItemValues[0]);
		reminderItem.setRemindText(reminderItemValues[1]);
		reminderItem.setScope(reminderItemValues[2]);
		// create android location object
		Location location = new Location("reverseGeocoded");
		location.setLatitude(Double.parseDouble(reminderItemValues[3]));
		location.setLongitude(Double.parseDouble(reminderItemValues[4]));
		reminderItem.setLocation(location);
		return reminderItem;
	}

	public static ArrayList<ReminderItem> getReminderList(Context context) {

		ArrayList<ReminderItem> reminderList;
		

		// restore from shared preferences
		reminderList = new ArrayList<ReminderItem>();

		// load all reminders from shared preferences
		prefs = PreferenceManager.getDefaultSharedPreferences(context);

		Map<String, ?> keys = prefs.getAll();

		for (Map.Entry<String, ?> entry : keys.entrySet()) {
			ReminderItem reminderItem = Utility.parse(entry.getValue()
					.toString());
			reminderList.add(reminderItem);
		}

		return reminderList;
	}
	
	public static ReminderItem getReminderItemByKey(Context context, String key) {

		ArrayList<ReminderItem> reminderList;
		

		// restore from shared preferences
		reminderList = new ArrayList<ReminderItem>();

		// load all reminders from shared preferences
		prefs = PreferenceManager.getDefaultSharedPreferences(context);

		Map<String, ?> keys = prefs.getAll();

		for (Map.Entry<String, ?> entry : keys.entrySet()) {
			ReminderItem reminderItem = Utility.parse(entry.getValue().toString());
			System.out.println("******gelen: " + key);
			System.out.println("******prefs: " + entry.getKey());
			if(key.equals(entry.getKey())){
				return reminderItem;
			}
		}
		
		return null;
	}
	
	public static Location getKnownLocation(Context context) {
		locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
		
		Location networkKnownLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		Location gpsKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		if(networkKnownLocation.getTime() > gpsKnownLocation.getTime()){
			return networkKnownLocation;
		} else{
			return gpsKnownLocation;
		}
	}
	
	/**
	 * Removes reminder from list that feeds  remindListView and sharedPreferences.
	 * @param positionToRemove
	 */
	public static void deleteReminderItem(ReminderItem item, Context context) {
		if(item != null){
			prefs = PreferenceManager.getDefaultSharedPreferences(context);
			
			SharedPreferences.Editor editor = prefs.edit();
			editor.remove(item.getKey());
			editor.commit();
		}
		
		
	}
}
